﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using IronTrishul.Discovery;
using System.Dynamic;
using System.Reflection;

namespace IronTrishul.Web
{
    public class Http : DynamicObject
    {
        public   enum ProxyType
        {
            SoapProxy, Wsdl, Html, WCF, REST,Unknown
        }
        Uri defautltSite = new Uri("http://www.google.co.in");

        internal System.Web.Services.Protocols.SoapHttpClientProtocol legacyProxy;
       

        public Uri DefautltSite
        {
            get { return defautltSite; }
            set { defautltSite = value; }
        }
  
        dynamic proxy;

        private dynamic Proxy
        {
            get { return proxy; }
            set { proxy = value; }
        }

        CodeCompileUnit wsdlCompiledCode;

        public CodeCompileUnit WsdlCompiledCode
        {
            get { return wsdlCompiledCode; }
            set { wsdlCompiledCode = value; }
        }
        WSDLManager controller;

        public WSDLManager Controller
        {
            get { return controller; }
            set { controller = value; }
        }
        //public Http(WSDLManager manager)
        //{

        //    controller = manager;
        //    Initialise(controller);
        //}
        public Http(Uri url):this(url, ProxyType.Wsdl)
        {
        }
        public Http(Uri url, ProxyType type)
        {
            defautltSite = url;
            controller = new WSDLManager(url.AbsolutePath, null, null);
            Initialise(controller);
        }
        MemberInfo[] linkMembers;
        //MemberInfo[] wsdlMembers;
        ProxyType pt = ProxyType.Unknown;
        private void Initialise(WSDLManager mgr)
        {
            //if only a wsdl only...
            try
            {
                proxy = controller.GetProxy();
                pt = ProxyType.Wsdl;
            }
            catch (Exception ef)
            {
                proxy = new DynamicLink(defautltSite);
                //throw new Exception("This is not a valid wsdl, see inner exception for details", ef);
                pt = ProxyType.Html;
            }
            //Need more filter ... dynamic ???
            linkMembers = proxy.GetType().GetMembers();

        }

        public override bool TryGetMember(GetMemberBinder binder,
             out object result)
        {
            string name = binder.Name;
            result = null;
            MemberInfo[] members = proxy.GetType().GetMember(name);
            if (members.Length > 0)
            {
                if (members[0].MemberType == MemberTypes.Property)
                {

                }
                result = members[0];
            }
            else
            {
                base.TryGetMember(binder, out result);
            }
            return true;
        }
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {


           return  proxy.Invoke(binder.Name, args, out result); 
           // return base.TryInvokeMember(binder, args, out result);
        }

    }
}
